﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ccit.Utils
{
    /// <summary>
    /// 创建XML 数据类
    /// </summary>
    public class CreateXmlData
    {
        private StringBuilder m_sb;
        private string m_sRoot;
        private string m_sNode;

        /// <summary>
        ///		构造函数
        /// </summary>
        public CreateXmlData()
        {
            m_sb = new StringBuilder();
        }

        /// <summary>
        ///		为XML数据添加根节点
        /// </summary>
        /// <param name="RootName">
        ///		根节点名称
        /// </param>
        public void CreateRoot(string RootName)
        {
            m_sRoot = RootName;
            m_sb.Append("<");
            m_sb.Append(m_sRoot);
            m_sb.Append(">");
        }

        /// <summary>
        ///		为XML数据添加带有属性的根节点
        /// </summary>
        /// <param name="RootName">
        ///		根节点名称
        /// </param>
        /// <param name="AttributeNameWithComma">
        ///		以逗号分割的属性名称
        /// </param>
        /// <param name="AttributeValueWithComma">
        ///		以逗号分割的属性值
        /// </param>
        public void CreateRoot(string RootName, string AttributeNameWithComma, string AttributeValueWithComma)
        {
            m_sRoot = RootName;
            m_sb.Append("<");
            m_sb.Append(m_sRoot);

            string[] sAttrName = AttributeNameWithComma.Split(char.Parse(","));
            string[] sAttrValue = AttributeValueWithComma.Split(char.Parse(","));
            int iAttrNameLength = sAttrName.Length;
            int iAttrValueLength = sAttrValue.Length;
            for (int i = 0; i < iAttrNameLength; i++)
            {
                // 如果属性名称长度已经超过属性值的长度,则中止程序
                if (i < iAttrValueLength)
                    m_sb.Append(" " + sAttrName[i] + "=\"" + sAttrValue[i] + "\"");
                else
                    break;
            }
            m_sb.Append(">");
        }

        /// <summary>
        ///		为XML数据添加一级子节点
        /// </summary>
        /// <param name="NodeName">
        ///		一级子节点名称
        /// </param>
        public void CreateNode(string NodeName)
        {
            m_sNode = NodeName;
            m_sb.Append("<");
            m_sb.Append(m_sNode);
            m_sb.Append(">");
        }

        /// <summary>
        ///		为XML数据添加带有属性的一级节点
        /// </summary>
        /// <param name="NodeName">
        ///		一级节点名称
        /// </param>
        /// <param name="AttributeNameWithComma">
        ///		以逗号分割的属性名称
        /// </param>
        /// <param name="AttributeValueWithComma">
        ///		以逗号分割的属性值
        /// </param>
        public void CreateNode(string NodeName, string AttributeNameWithComma, string AttributeValueWithComma)
        {
            m_sNode = NodeName;
            m_sb.Append("<");
            m_sb.Append(m_sNode);

            string[] sAttrName = AttributeNameWithComma.Split(char.Parse(","));
            string[] sAttrValue = AttributeValueWithComma.Split(char.Parse(","));
            int iAttrNameLength = sAttrName.Length;
            int iAttrValueLength = sAttrValue.Length;
            for (int i = 0; i < iAttrNameLength; i++)
            {
                // 如果属性名称长度已经超过属性值的长度,则中止程序
                if (i < iAttrValueLength)
                    m_sb.Append(" " + sAttrName[i] + "=\"" + sAttrValue[i] + "\"");
                else
                    break;
            }
            m_sb.Append(">");
        }

        /// <summary>
        ///		增加一个一级节点
        /// </summary>
        /// <param name="sNodeName">
        ///		节点名称
        /// </param>
        /// <param name="sNodeValue">
        ///		节点值
        /// </param>
        public void AddNewNode(string sNodeName, string sNodeValue)
        {
            m_sb.Append("<");
            m_sb.Append(sNodeName);
            m_sb.Append("><![CDATA[");
            m_sb.Append(sNodeValue);
            m_sb.Append("]]></");
            m_sb.Append(sNodeName);
            m_sb.Append(">");
        }

        /// <summary>
        ///		为XML数据添加节点
        /// </summary>
        /// <param name="NodeName">
        ///		节点标签的名称
        /// </param>
        /// <param name="NodeValue">
        ///		节点标签的值,该方法将为NodeValue加上CDTAT标签,所以编码人员不需要传入经过CDATA的NodeValue
        /// </param>
        public void CreateElement(string NodeName, string NodeValue)
        {
            m_sb.Append("<");
            m_sb.Append(NodeName);
            m_sb.Append("><![CDATA[");
            m_sb.Append(NodeValue);
            m_sb.Append("]]></");
            m_sb.Append(NodeName);
            m_sb.Append(">");
        }

        /// <summary>
        ///		为XML数据追加XML结构的数据
        /// </summary>
        /// <param name="XmlData">
        ///		XML结构的数据
        /// </param>
        public void AppendXMLData(string XmlData)
        {
            m_sb.Append(XmlData);
        }

        /// <summary>
        ///		该函数返回构造好的XML数据
        /// </summary>
        /// <returns>
        ///		构造好的XML数据
        /// </returns>
        public string ReturnXML()
        {
            m_sb.Append("</");
            m_sb.Append(m_sNode);
            m_sb.Append(">");
            m_sb.Append("</");
            m_sb.Append(m_sRoot);
            m_sb.Append(">");

            return m_sb.ToString();
        }
    }
}
